package com.example.tool.util.ane;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.security.NoSuchAlgorithmException;

public class AneApiUtil {

    private static String URL = "http://opc.ane56.com/aneop/services/logisticsQuery/new/query";
    private static String APPKEY = "e10adc3949ba59abbe56e057f20f883e";
    private static String CODE = "ANENG";


    public static JSONObject queryLogistics(String waybillNo) {
//        log.info("安能快递查询参数[{}]", JSON.toJSONString(request));
        JSONArray jsonArray = new JSONArray();
        try {
            JSONObject jsonObject = new JSONObject();
            JSONObject params = new JSONObject();
            params.put("ewbNo", waybillNo);
            jsonObject.put("params", params.toJSONString());
            String digest = Base64.encode(SecureUtil.md5("{\"ewbNo\":\"" + waybillNo+ "\"}" + CODE + APPKEY));
            jsonObject.put("digest", digest);
            jsonObject.put("code", CODE);
            String timestamp = Long.toString(System.currentTimeMillis());
            jsonObject.put("timestamp", timestamp);
            String res = HttpRequest.post(URL).header("Content-type", "application/json").body(jsonObject.toJSONString()).execute().body();
            JSONObject resObject = JSONObject.parseObject(res);
            return resObject;
//            jsonArray = resObject.getJSONObject("resultInfo").getJSONArray("traces");
        } catch (Exception e) {
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(queryLogistics("233565416"));
    }
}
